<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="keyword">
        <el-input
            v-model="queryParams.keyword"
            placeholder="请输入流程标题"
            clearable
            style="width: 300px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="procInstanceList">
      <el-table-column label="序号" align="center" type="index" width="100"/>
      <el-table-column
          width="300"
          label="流程标题"
          align="left"
          prop="formName"
          :show-overflow-tooltip="true"
      />
      <el-table-column width="200" show-overflow-tooltip label="流程实例ID" align="center" prop="processInstanceId"/>
      <el-table-column width="200" show-overflow-tooltip label="业务单号" align="center" prop="businessKey"/>
      <el-table-column width="150" label="提交人" align="left" prop="startedUserName" />
      <el-table-column width="150" show-overflow-tooltip label="提交部门" align="left" prop="startedUserDeptName" />
      <el-table-column width="200" label="提交时间" align="center" prop="startTime" />

      <el-table-column  width="150" fixed="right" label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip class="box-item" effect="dark" content="流程图" placement="top" >
            <el-button
                link
                type="primary"
                icon="Picture"
                @click="handleBpmnPreview(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="box-item" effect="dark" content="审批记录" placement="top" >
            <el-button
                link
                type="primary"
                icon="ChatLineSquare"
                @click="handleApproveHistoryPreview(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="box-item" effect="dark" content="流程变量" placement="top" >
            <el-button
                link
                type="primary"
                icon="Operation"
                @click="handleProcessVarsPreview(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip class="box-item" effect="dark" content="终止" placement="top" v-if="!scope.row.endTime" >
            <el-button
                link
                type="danger"
                icon="CircleClose"
                @click="handleExec('stop', scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 预览流程对话框 -->
    <el-dialog width="1000px"
               :title="bpmnViewerTitle"
               v-model="bpmnViewerVisible"
               append-to-body
               class="bpmn-viewer-body">
      <div style="height: 500px;">
        <BpmnViewer :proc-inst-id="previewProcInstId" ref="bpmnViewerRef" />
      </div>
    </el-dialog>

    <!-- 预览流审批记录对话框 -->
    <el-dialog
        :title="approveHistoryTitle"
        v-model="approveHistoryVisible"
        width="1000px"
        append-to-body>
      <div>
        <ApproveHistory :proc-inst-id="previewProcInstId" />
      </div>
    </el-dialog>
    <!-- 预览流程变量对话框 -->
    <el-dialog
        :title="processVarsTitle"
        v-model="processVarsVisible"
        width="1000px"
        append-to-body>
      <div>
        <process-vars :proc-inst-id="previewProcInstId" />
      </div>
    </el-dialog>

  </div>
</template>

<script setup name="ProcInstance">
import {completeTask, queryMyProcessInstance, stopProcess} from "@/api/flowable/bpmn/modelInfo";
import ApproveHistory from "@/views/flowable/components/ApproveHistory.vue";
import BpmnViewer from "@/views/flowable/components/BpmnViewer.vue";
import ProcessVars from "@/views/flowable/components/ProcessVars.vue";

const { proxy } = getCurrentInstance();

const previewModelKey = ref('');
const previewProcInstId = ref('');
const bpmnViewerTitle = ref('');
const bpmnViewerVisible = ref(false);
const processVarsTitle = ref('');
const processVarsVisible = ref(false);

// 遮罩层
const loading = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 表格数据
const procInstanceList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const approveHistoryTitle = ref('');
const approveHistoryVisible = ref(false);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined
  }
})

const {queryParams} = toRefs(data);


const getList = () => {
  loading.value = true;
  queryMyProcessInstance(queryParams.value).then(response => {
    procInstanceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleBpmnPreview (row){
  bpmnViewerTitle.value = "流程预览-" + row.formName;
  bpmnViewerVisible.value = true;
  previewProcInstId.value = row.processInstanceId;
}

function handleApproveHistoryPreview (row) {
  approveHistoryVisible.value = true;
  previewProcInstId.value = row.processInstanceId;
  approveHistoryTitle.value = `审批记录 - ${row.formName}`;
}

function handleProcessVarsPreview (row) {
  processVarsVisible.value = true;
  previewProcInstId.value = row.processInstanceId;
  processVarsTitle.value = `流程变量 - ${row.formName}`;
}

getList();
const cancel = () => {
  open.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}


/** 执行按钮操作 */
const handleExec = (exeType, row) => {
  proxy.$modal.confirm("确定要审批当前流程吗！").then(function (){
    loading.value = true;
    stopProcess({taskId: row.taskId, processInstanceId: row.processInstanceId}).then(res => {
      proxy.$modal.msgSuccess(res.msg);
      getList();
      loading.value = false;
    }).catch(e => {
      loading.value = false;
    });
  });
}

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
}

</script>
